package com.product.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.ArgpFactory;
import com.product.system.domain.FactoryProvince;
import com.product.system.service.ArgpFactoryService;
import com.product.system.service.FactoryProvinceService;
import com.product.system.utils.Result;
//import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("argpFactory")
public class ArgpFactoryController {

    @Autowired
    ArgpFactoryService argpFactoryService;

    @Autowired
    FactoryProvinceService factoryProvinceService;

    @RequestMapping("getExcel")
    public void getExcel(HttpServletResponse response) throws IOException {
       String fileName= URLEncoder.encode("工厂信息","UTF-8").replaceAll("\\+","%20");
       response.setCharacterEncoding("UTF-8");
       response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
       response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<ArgpFactory> list= argpFactoryService.listGetArgpFactory();
        EasyExcel.write(response.getOutputStream(),ArgpFactory.class).sheet("工厂信息").doWrite(list);
    }

    @RequestMapping("addOrUpdate")
    public Result addOrUpdate(@RequestBody ArgpFactory argpFactory){
        if(argpFactory.getId()!=null){
            argpFactoryService.updateFactory(argpFactory);

        }else {
            String s = IdUtil.fastSimpleUUID();
           // argpFactory.setId(s);//雪花编号
            argpFactory.setCreateDate(new Date());
            argpFactory.setState(1);
            argpFactory.setDeleteState(0);
            argpFactoryService.addSave(argpFactory);
        }
        return Result.success();
    }

    /**
     * 列表
     * @param argpFactory
     * @param pageNum
     * @return
     */
    @RequestMapping("list")
    public Result list(@RequestBody ArgpFactory argpFactory,Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<ArgpFactory> list=argpFactoryService.listArgpFactory(argpFactory);
        PageInfo<ArgpFactory> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @RequestMapping("deleteById")
    public Result deleteById(String id){
        argpFactoryService.updateId(id);
        return Result.success();
    }

    /**
     * 添加
     * @param argpFactory
     * @return
     */
    @RequestMapping("addSave")
    public Result addSave(@RequestBody ArgpFactory argpFactory){
        argpFactoryService.addArgpFactory(argpFactory);
        return Result.success();
    }



}
